// Copyright (C) ZHENG Zhong <heavyzheng nospam-at gmail D0T com>
//
// $Id: dummy_logging.cpp 230 2008-09-15 15:50:18Z heavyzheng $
//

//! \example dummy_logging.cpp
//!
//! This example shows how to use the logger object to log messages. All messages are written to a
//! static logger defined in an anonymous namespace. The logger exists only inside this translation
//! unit.

#include "dummy_logging.hpp"
#include "config_multi_dests.hpp"

#ifdef ZZHENG_MSVC
#  pragma warning(push)
#  pragma warning(disable: 4245 4511 4512)
#endif // ZZHENG_MSVC

#include <boost/lexical_cast.hpp>

#ifdef ZZHENG_MSVC
#  pragma warning(pop)
#endif // ZZHENG_MSVC

#include <string>
#include <stdexcept>
#include <memory>

namespace zzheng {
namespace example {

    namespace {

        // Create a logger in an anonymous namespace. It is recommended that logger objects be
        // static variables only inside translation units, and be hidden from outside.
        static logging::logger log_ = get_logger_to_multi_dests("dummy_logging");

    } // anonymous namespace

    ////////////////////////////////////////////////////////////////////////////////////////////////

    void dummy_logging() {

        //
        // Log strings.
        //
        log_.info ("Logger name      : " + log_.name());
        log_.info ("Logger threshold : " + log_.threshold().str());
        log_.trace("Message at trace level.");
        log_.debug("Message at debug level.");
        log_.info ("Message at info  level.");
        log_.warn ("Message at warn  level.");
        log_.error("Message at error level.");
        log_.fatal("Message at fatal level.");

        //
        // Log exceptions (std::logic_error derives from std::exception).
        //
        log_.error_ex(std::logic_error("logic_error #1"));
        try {
            log_.info("About to throw a logic_error...");
            throw std::logic_error("logic_error #2");
        } catch (const std::exception& ex) {
            log_.info("Expecting to catch a logic_error...");
            log_.info_ex(ex);
        }

        //
        // Log ranges from containers.
        //
        std::vector<int> numbers;
        for (int i = 0; i < 3; ++i) {
            numbers.push_back(i);
        }
        log_.info_range("Range should contain 3 integers", numbers.begin(), numbers.end());

        std::vector<const char*> strs(3, "dummy");
        log_.info_range("Range should contain 3 const char*'s", strs.begin(), strs.end());

        std::map<int, std::string> num_to_str;
        for (int i = 0; i < 3; ++i) {
            num_to_str.insert( std::make_pair(i, "value " + boost::lexical_cast<std::string>(i)) );
        }
        log_.info_range("Range should contain 3 pairs", num_to_str.begin(), num_to_str.end());

        //
        // Log pointers.
        //
        std::auto_ptr<std::exception> ptr_1(new std::logic_error("dummy"));
        std::auto_ptr<const std::exception> ptr_2(new std::logic_error("dummy"));
        log_.info_ptr("The non-const logic error pointer", ptr_1.get());
        log_.info_ptr("The const logic error pointer", ptr_2.get());
    }

} // namespace zzheng::example
} // namespace zzheng



